<?php
use yii\helpers\Url;
?>
<div class="layui-form" id="category" style="padding: 20px 30px 0 0;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span> 分类名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="name" name="name" required="" lay-verify="name"
                       autocomplete="off" class="layui-input">
                <input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->csrfToken?>">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="pwd" class="layui-form-label">
                上级分类
            </label>
            <div class="layui-input-inline">
                <select name="fid" id="fid">
                    <option value="0">--顶级分类--</option>
                    <?php foreach ($gradeTree as $list): ?>
                        <option value="<?= $list['id']?>"<?=isset($fid)&&$fid==$list['id']?'selected':''?>><?= $list['name'] ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span> 不选择则是顶级分类
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="add" lay-submit="">
                增加
            </button>
        </div>
    </form>
</div>

<?php $this->beginBlock('footer') ?>
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form ,layer = layui.layer;
        //自定义验证规则示例
        form.verify({
            name: function(value){
                if(!value){
                    return '名称不能为空';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            layer.load(3);
            $.post('<?= Url::to([$this->context->id.'/create']) ?>', data.field, function (res) {
                layer.closeAll();
                if (res.status === 200) {
                    layer.msg(res.msg, {icon: 1,time: 1500}, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.location.reload();
                    })
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1500})
                }
            }, 'json');
            return false;
        });
    });
</script>
<?php $this->endBlock() ?>
